import Vue from 'vue'
import App from './App'

// import router from "./router";
import store from './store'
import schema from 'async-validator'
import dialog from './utils/dialog'
import cookie from '@/utils/store/cookie'
import cuCustom from '@/components/colorui/components/cu-custom.vue'
//app迁start
import uView from '@/uni_modules/uview-ui'
Vue.use(uView)
import pageMixin from './mixins/pageMixin'
Vue.mixin(pageMixin)
// 全局mixins，用于实现setData等功能';
import Mixin from './polyfill/mixins'
Vue.mixin(Mixin)
// 全局分享
import ShareMixin from '@/mixins/share'
Vue.mixin(ShareMixin)
import BaseToast from '@/components/BaseToast/BaseToast.vue'
Vue.component('BaseToast', BaseToast)
import empty from '@/components/empty'
Vue.component('empty', empty)
//全局过滤器
import * as filters from '@/utils/filters'
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})
//app 迁end

import { parseRoute, _router, parseQuery } from '@/utils'
import { VUE_APP_RESOURCES_URL, BaseUrl } from '@/common/config.js'
Vue.component('cu-custom', cuCustom)
Vue.config.productionTip = false
Vue.config.devtools = process.env.NODE_ENV !== 'production'

Vue.prototype.$validator = function (rule) {
  return new schema(rule)
}

Vue.config.productionTip = false
App.mpType = 'app'
Vue.prototype.$store = store

// app迁--start
const prePage = () => {
  let pages = getCurrentPages()
  let prePage = pages[pages.length - 2]
  return prePage
}
const curPage = () => {
  let pages = getCurrentPages()
  let curPage = pages[pages.length - 1]
  return curPage
}
Vue.prototype.$prePage = prePage
Vue.prototype.$curPage = curPage

//发布时间过滤器
Vue.filter('createTime', function (dateTimeStamp) {
  var result = ''
  var padDate = function (va) {
    va = va < 10 ? '' + va : va
    return va
  }
  var minute = 1000 * 60
  var hour = minute * 60
  var day = hour * 24
  var halfamonth = day * 15
  var month = day * 30
  var now = new Date().getTime()
  var oldTime = new Date(
    dateTimeStamp.replace(/-/g, '/').replace(/T/g, ' ').substr(0, 19)
  ).getTime()
  var diffValue = now - oldTime
  if (diffValue < 0) {
    return
  }
  var monthC = diffValue / month
  var weekC = diffValue / (7 * day)
  var dayC = diffValue / day
  var hourC = diffValue / hour
  var minC = diffValue / minute
  //    if(monthC >= 1) {
  //        result = "" + parseInt(monthC) + "月前";
  //    } else if(weekC >= 1) {
  //        result = "" + parseInt(weekC) + "周前";
  //    } else
  if (dayC >= 3) {
    var regTime = dateTimeStamp.replace(/-/g, '/').replace(/T/g, ' ')
    var time = regTime.substr(0, 19)
    var value = new Date(time)
    var year = value.getFullYear()
    month = padDate(value.getMonth() + 1)
    day = padDate(value.getDate())
    hour = padDate(value.getHours())
    var minutes = padDate(value.getMinutes())
    var seconds = padDate(value.getSeconds())
    if (hour < 10) hour = '0' + hour
    if (minutes < 10) minutes = '0' + minutes
    result = year + '.' + month + '.' + day + ' ' + hour + ':' + minutes
  } else if (dayC >= 1) {
    result = '' + parseInt(dayC) + '天前'
  } else if (hourC >= 1) {
    result = '' + parseInt(hourC) + '小时前'
  } else if (minC >= 1) {
    result = '' + parseInt(minC) + '分钟前'
  } else {
    result = '刚刚'
  }

  return result
})
// app迁--end

const app = new Vue({
  ...App,
  store,
})

Object.defineProperty(Vue.prototype, '$yrouter', {
  get() {
    return _router
  },
})

Object.defineProperty(Vue.prototype, '$yroute', {
  get() {
    return this._route
  },
})

Vue.prototype.$BaseUrl = BaseUrl
Vue.component('cu-custom', cuCustom)

let deviceType = ''
// #ifdef APP-PLUS
// App平台编译的代码
deviceType = 'app'
Vue.prototype.$platform = uni.getSystemInfoSync().platform
// #endif

// #ifdef MP-WEIXIN
// 微信小程序编译的代码
deviceType = 'routine'
// #endif

// !!! ps  不建议在 template 中使用 $deviceType 去判断当前环境，很有可能出现 $deviceType 为 undefined 导致判断出错的问题，可以在 script 模块中正常使用
// 建议通过 store 去获取 $deviceType 可以保证 template 中取到的值有效
// import { mapState, mapMutations, mapActions } from 'vuex';
// computed: {
// 		...mapState(['$deviceType'])
// },

// #ifdef H5
// H5编译的代码

import {
  wechat,
  clearAuthStatus,
  oAuth,
  auth,
  toAuth,
  pay,
  openAddress,
  openShareAll,
  openShareAppMessage,
  openShareTimeline,
  wechatEvevt,
  ready,
  wxShowLocation,
} from '@/libs/wechat'

import { isWeixin } from '@/utils'
const CACHE_KEY = 'clear_0.0.1'

if (!cookie.has(CACHE_KEY)) {
  cookie.clearAll()
  cookie.set(CACHE_KEY, 1)
}

var urlSpread = parseQuery()['spread']
if (urlSpread) {
  cookie.set('spread', urlSpread)
  console.log('------11111111111---------spread=', urlSpread)
}

// #endif

// #ifdef H5
// H5编译的代码
// 判断是否是微信浏览器
async function init() {
  if (isWeixin()) {
    deviceType = 'weixin'
    let wechatInit = wechat()
    if (wechatInit) {
      await oAuth()
    }
  } else {
    deviceType = 'weixinh5'
  }
}
init()
// #endif

Vue.prototype.$deviceType = deviceType

Vue.mixin({
  onLoad() {
    const { $mp } = this.$root
    this._route = parseRoute($mp)
  },
  onShow() {
    _router.app = this
    _router.currentRoute = this._route
  },
  // 这里为了解决 .vue文件中 template 无法获取 VUE.prototype 绑定的变量
  computed: {
    $VUE_APP_RESOURCES_URL() {
      return VUE_APP_RESOURCES_URL
    },
  },
})

store.commit('updateDevicetype', deviceType)

app.$mount()
